
bool equalFloat(float a, float b){
	return abs(a-b)<0.01;
}

struct Vector {
    vec2 from;	//起始点
    vec2 to;	//结束点
	vec2 v;	//向量
};

Vector makeVector(vec2 from, vec2 to){
	return Vector(from,to,(to-from));
}

Vector negativeVector(Vector v){
	return makeVector(v.to, v.from);
}

float productVecto(Vector a, Vector b){
	return a.v.x*b.v.y-b.v.x*a.v.y;
}

//计算向量ab与cd是否相交
bool intersected(vec2 a,vec2 b, vec2 c, vec2 d){
	float ZERO = 0.001;

	Vector AC = makeVector(a, c);
	Vector AD = makeVector(a, d);
	Vector BC = makeVector(b, c);
	Vector BD = makeVector(b, d);
	Vector CA = makeVector(c, a);
    Vector CB = makeVector(c, b);
    Vector DA = makeVector(d, a);
    Vector DB = makeVector(d, b);

	return (productVecto(AC, AD) * productVecto(BC, BD) <= ZERO) &&
		(productVecto(CA, CB) * productVecto(DA, DB) <= ZERO);
}

//RGB颜色空间与HSBC颜色空间转换
vec3 rgb2hsb( in vec3 c ){
    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz),
                 vec4(c.gb, K.xy),
                 step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r),
                 vec4(c.r, p.yzx),
                 step(p.x, c.r));
    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)),
                d / (q.x + e),
                q.x);
}

vec3 hsb2rgb( in vec3 c ){
    vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),
                             6.0)-3.0)-1.0,
                     0.0,
                     1.0 );
    rgb = rgb*rgb*(3.0-2.0*rgb);
    return c.z * mix(vec3(1.0), rgb, c.y);
}

//Sobel卷积核
vec3 SobelX[3];
vec3 SobelY[3];
void initSobel(){
  SobelX[0] = vec3(-1, -2, -1);
  SobelX[1] = vec3(0, 0, 0);
  SobelX[2] = vec3(1, 2, 1);

  SobelY[0] = vec3(1, 0, -1);
  SobelY[1] = vec3(2, 0, -2);
  SobelY[2] = vec3(1, 0, -1);
}